package ua.com.globallogic.javatraining.iryna_chorna.univercity.people;

import java.util.Arrays;

import ua.com.globallogic.javatraining.iryna_chorna.univercity.structure.Course;

/**
 * This class will create a student object, identified by name. 
 * The student can enroll for <code>course</code>s and attend lectures.
 */
public class Student extends Person {
	protected  final int MAX_NUMBER_OF_COURSES = 100;
	private Course[] coursesEnrolled = new Course[MAX_NUMBER_OF_COURSES];
	private int enrolledCoursesCount;
	
	/**
	 * Constructs a newbie student who have no enrollments yet
	 * @param name studen't full name
	 */
	public Student(Name name) {
		this.name = name;
		enrolledCoursesCount = 0;
	}

	/** 
	 * Student attends lecture if it is included to their enrollment list 
	 * @param course course to which the lecture belongs
	 * @return true if teacher successfully attended lecture
	 */
	@Override
	public boolean attendLecture(Course course) {
		if (isEnrolledFor(course)) {
			System.out.println("Student listens to a teacher during a lecture");
			return true;
		} else {
			System.out.println("Student can't attend lectures that are not in their enrollment list");
			return false;
		}
	}
	
	/**
	 * Adds a course to this student's enrollment list
	 * @param course course to add
	 * @return true if the student was successfully enrolled for course
	 */
	public boolean enrollForCourse(Course course) {
		if (!isEnrolledFor(course) && (enrolledCoursesCount < MAX_NUMBER_OF_COURSES)) {
			coursesEnrolled[enrolledCoursesCount] = course;
			enrolledCoursesCount++;
			return true;
		}
		return false;
	}
	
	/**
	 * Checks whether student is enrolled for a course
	 * @param course course to check 
	 * @return true if student is enrolled for the course
	 */
	protected boolean isEnrolledFor(Course course) {
		if (Arrays.asList(coursesEnrolled).contains(course)) {
			return true;
		}
		return false;
	}
}
